package com.dong.cutesms.components;

import java.text.MessageFormat;

import com.dong.cutesms.R;
import com.dong.cutesms.activitys.MainActivity;
import com.dong.cutesms.activitys.ShowMainSmsActivity;
import com.dong.cutesms.models.PeopleModel;
import com.dong.cutesms.utils.SmsUtils;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import android.support.v4.*;

import java.text.MessageFormat;
import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.telephony.SmsMessage;
import android.text.ClipboardManager;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver {

	public static final String SMS_EXTRA_NAME = "pdus";
	public static final String SMS_URI = "content://sms";

	
	// Change the password here or give a user possibility to change it
	public static final byte[] PASSWORD = new byte[] { 0x20, 0x32, 0x34, 0x47,
			(byte) 0x84, 0x33, 0x58 };
	
	Context context;
	
	
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public void onReceive(Context context, Intent intent) {
		// Get SMS map from Intent
		this.context = context;
		Bundle extras = intent.getExtras();
		SmsUtils smsUtils = new SmsUtils(context.getContentResolver(), context);
		String messages = "";
		String address = "";
		if (extras != null) {
			// Get received SMS array
			Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

			ContentResolver contentResolver = context.getContentResolver();

			for (int i = 0; i < smsExtra.length; ++i) {
				SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);

				String body = sms.getMessageBody().toString();
				address = sms.getOriginatingAddress();
				
				messages += body + "\n";

				updatePeopleTable(contentResolver,sms);
				
				//context.getPackageManager().setComponentEnabledSetting(arg0, arg1, arg2)
			}
			Log.d("TAG", "start function");

			// display notification
			String displayName = smsUtils.getContactDisplayNameByNumber(address);
			if(displayName.equals("?")){
				displayName = address;
			}
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification myNotification = new Notification(
					R.drawable.face3, messages,
					System.currentTimeMillis());
			
			String notificationTitle = displayName;
			String notificationText = messages.length() > 36 ? messages.substring(0, 33)+" ...":messages;
			Intent myIntent = new Intent(context,MainActivity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(context,
					0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
			myNotification.defaults |= Notification.DEFAULT_SOUND;
			myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
			myNotification.setLatestEventInfo(context, notificationTitle,
					notificationText, pendingIntent);
			notificationManager.notify(1, myNotification);
			
			//end of display notification.

		}

		// this.abortBroadcast();
	}

	
	
	private void updatePeopleTable(ContentResolver contentResolver,
			SmsMessage sms) {
		String body = sms.getMessageBody().toString();
		String address = sms.getOriginatingAddress();
		
		SmsUtils smsUtils = new SmsUtils(contentResolver, context);
		int threadID = smsUtils.getThreadIdByPhoneNumber(address);
		if(threadID == -1){
			
		}else{
			PeopleModel people = smsUtils.getPeopleByThreadID(threadID);
			people.hasSms ++;
			people.date = System.currentTimeMillis();
			people.recentMessage = body;
			smsUtils.updatePeople(people);
		}
		
	}
}
